package qz.lixinyu.modules.sys.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import qz.lixinyu.common.utils.PageUtils;
import qz.lixinyu.common.utils.Query;

import qz.lixinyu.modules.company.dao.QyCompanyDao;
import qz.lixinyu.modules.company.entity.QyCompanyEntity;
import qz.lixinyu.modules.company.service.QyCompanyService;
import qz.lixinyu.modules.sys.dao.QzDao;
import qz.lixinyu.modules.sys.dao.SysUserDao;
import qz.lixinyu.modules.sys.entity.QzEntity;
import qz.lixinyu.modules.sys.entity.SysUserEntity;

import qz.lixinyu.modules.sys.service.QzService;
import qz.lixinyu.modules.sys.service.SysUserService;


@Service("qzService")
public class QzServiceImpl extends ServiceImpl<QzDao, QzEntity> implements QzService {
    @Autowired
    private QzDao qzDao;
    @Autowired
    private QzService qzService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private QyCompanyDao qyCompanyDao;
    @Autowired
    private QyCompanyService qyCompanyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 构建分页查询条件，查询 QzEntity 数据
        IPage<QzEntity> page = null;
        // 查询条件
        QueryWrapper<QzEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank((String) params.get("username"))) {
            wrapper.eq("username", params.get("username"));
        }
        //TODO 如果是admin看全部  其他看自己的
        if(params.get("username").equals("admin")){
            page = this.page(
                    new Query<QzEntity>().getPage(params),
                    new QueryWrapper<QzEntity>()
            );
        }else {
            page = this.page(
                    new Query<QzEntity>().getPage(params),
                    new QueryWrapper<QzEntity>().eq(StringUtils.isNotBlank((String) params.get("username")),"username", params.get("username"))
            );
        }

//        // 查询 QzEntity 列表
        IPage<QzEntity> qzPage = null;
        if(params.get("username").equals("admin")){
            QueryWrapper queryWrapperAdmin = new QueryWrapper<>();
          qzPage = qzDao.selectPage(page, queryWrapperAdmin);
        }else {
            // 查询 QzEntity 列表
           qzPage = qzDao.selectPage(page, wrapper);
        }
        // 遍历 QzEntity 列表，关联用户信息和职位信息
        for (QzEntity qzEntity : qzPage.getRecords()) {
            // 查询用户信息
            SysUserEntity sysUserEntity = sysUserDao.selectOne(new QueryWrapper<SysUserEntity>()
                    .eq("username", qzEntity.getUsername()));
            qzEntity.setSysUserEntity(sysUserEntity);
            // 查询职位信息
            QyCompanyEntity qyCompanyEntity = qyCompanyDao.selectById(qzEntity.getCompanyId());
            qzEntity.setQyCompanyEntity(qyCompanyEntity);
        }
        return new PageUtils(qzPage);
    }

    /**
     * 同意录取
     * 1.更改user_zq的状态1同意
     * 2.变化qy_company的录取数量num+1
     * @param qzId
     */
    @Override
    public void approve(Long qzId) {
        //1.更改user_zq的状态1同意
        QzEntity qz = new QzEntity();
        qz.setId(qzId);
        qz.setStatus(1);//同意
        qzService.updateById(qz);
        //2.变化qy_company的录取数量num+1
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",qzId);
        QzEntity qzEntity = qzService.getOne(queryWrapper);
        //通过职位id拿到职位的投递人数/和总数   +1
        Long companyId = qzEntity.getCompanyId();
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",companyId);
        QyCompanyEntity qyCompanyEntity = qyCompanyService.getOne(queryWrapper1);
        Integer num = qyCompanyEntity.getNum();   // 录取人数
        Integer newNum =num+1;
        qyCompanyEntity.setNum(newNum);
        qyCompanyService.updateById(qyCompanyEntity);

    }

    /**
     * 拒绝简历
     * 1.user_qz更改状态为2拒绝

     * @param qzId
     */
    @Override
    public void reject(Long qzId) {
//1.更改user_zq的状态1同意
        QzEntity qz = new QzEntity();
        qz.setId(qzId);
        qz.setStatus(2);//拒绝
        qzService.updateById(qz);
    }

}